package com.changgou.web.filter;

import com.changgou.web.service.AuthService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class AuthFilter implements GlobalFilter, Ordered {
    @Autowired
    private AuthService authService;

    private static final String LOGIN_URL = "http://localhost:8001/api/oauth/toLogin";


    //配置过滤器的拦截方法,再网关中配置需要拦截所有请求
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        //获取request对象
        ServerHttpRequest request = exchange.getRequest();
        //获取response对象
        ServerHttpResponse response = exchange.getResponse();

        //1.判断当前的请求路径是否为登录请求,如果是，则直接放行
        String path = request.getURI().getPath();
        if ("/api/oauth/login".equals(path) || !UrlFilter.hasAuthorize(path)) {//配置用户登录请求以及用户登录跳转页面
            return chain.filter(exchange);//登录请求直接放行
        }
        //2.从cookie中获取jti的值,如果值不存在，则拒绝本次请求
        String jti = authService.getJtiFromCookie(request);
        if (StringUtils.isEmpty(jti)) {
            //jti为空,则直接拒绝本次访问
            /*response.setStatusCode(HttpStatus.UNAUTHORIZED);//向客户端返回无权限校验码
            response.setComplete();//结束本次响应*/

            //如果不是登录请求，记录当前访问的连接，路由到登录页面，等用户登陆完成后，则再重新回到之前的请求链接
            return this.toLoginPage(LOGIN_URL + "?FROM=" + request.getURI().getPath(), exchange);
        }
        //3.根据jti从redis中获取jwt令牌的信息,如果不存在,则拒绝本次请求
        String jwt = authService.getJwtFromRedis(jti);
        if (StringUtils.isEmpty(jwt)) {
            //jwt不存在,则直接拒绝本次访问
           /* response.setStatusCode(HttpStatus.UNAUTHORIZED);//向客户端返回无权限校验码
            response.setComplete();//结束本次响应*/

            //如果不是登录请求，记录当前访问的连接，路由到登录页面，等用户登陆完成后，则再重新回到之前的请求链接
            return this.toLoginPage(LOGIN_URL + "?FROM=" + request.getURI().getPath(), exchange);
        }
        //4.对当前的请求对象进行增强,让他会携带令牌信息
        request.mutate().header("Authorization", "Bearer " + jwt);
        return chain.filter(exchange);
    }

    //记录当前的路由信息并跳转到登录页面
    private Mono<Void> toLoginPage(String loginUrl, ServerWebExchange exchange) {
        //获取httpResponse对象,进行页面跳转
        ServerHttpResponse response = exchange.getResponse();
        //告诉浏览器要发送一个重定向的请求
        response.setStatusCode(HttpStatus.SEE_OTHER);
        //重定向到loginUrl地址
        response.getHeaders().set("Location",loginUrl);
        return response.setComplete();
    }

    //配置过滤器的执行顺序
    @Override
    public int getOrder() {
        return 0;
    }
}
